/*
 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H
#define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H

#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <refbase.h>
#include <surface_type.h>
#ifndef ROSEN_CROSS_PLATFORM
#include <surface.h>
#endif

#ifdef NEW_RENDER_CONTEXT
#include "render_backend/rs_render_surface.h"
#else
#include "platform/drawing/rs_surface.h"
#endif
#include "rs_irender_client.h"
#include "variable_frame_rate/rs_variable_frame_rate.h"
#include "vsync_receiver.h"
#include "rs_hgm_config_data.h"

namespace Keels {
namespace Rosen {
struct DataBaseRs {
    int32_t appPid = -1;
    int32_t eventType = -1;
    int32_t versionCode = -1;
    int64_t uniqueId = 0;
    int64_t inputTime = 0;
    int64_t beginVsyncTime = 0;
    int64_t endVsyncTime = 0;
    bool isDisplayAnimator = false;
    std::string sceneId;
    std::string versionName;
    std::string bundleName;
    std::string processName;
    std::string abilityName;
    std::string pageUrl;
    std::string sourceType;
    std::string note;
};

class SurfaceCaptureCallback {
public:
    SurfaceCaptureCallback() {}
    virtual ~SurfaceCaptureCallback() {}
    virtual void OnSurfaceCapture(std::shared_ptr<Media::PixelMap> pixelmap) = 0;
};

class RSB_EXPORT RSRenderServiceClient {
public:
    RSRenderServiceClient() = default;
    virtual ~RSRenderServiceClient() = default;

    RSRenderServiceClient(const RSRenderServiceClient&) = delete;
    void operator=(const RSRenderServiceClient&) = delete;

    static std::shared_ptr<RSRenderServiceClient> CreateRenderServiceClient();
#ifdef NEW_RENDER_CONTEXT
    std::shared_ptr<RSRenderSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config);
#else
    std::shared_ptr<RSSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config);
#endif
    std::shared_ptr<VSyncReceiver> CreateVSyncReceiver(const std::string& name,
        const std::shared_ptr<Keels::AppExecFwk::EventHandler>& looper = nullptr, uint64_t id = 0,
        NodeId windowNodeId = 0);

    std::shared_ptr<Media::PixelMap> CreatePixelMapFromSurfaceId(uint64_t surfaceid, const Rect& srcRect);

#ifndef ROSEN_CROSS_PLATFORM
#if defined(NEW_RENDER_CONTEXT)
    std::shared_ptr<RSRenderSurface> CreateRSSurface(const sptr<Surface>& surface);
#else
    std::shared_ptr<RSSurface> CreateRSSurface(const sptr<Surface>& surface);
#endif
    ScreenId CreateVirtualScreen(const std::string& name, uint32_t width, uint32_t height, sptr<Surface> surface,
        ScreenId mirrorId, int32_t flags, std::vector<NodeId> whiteList = {});

    int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface);

    int32_t SetVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector);
#endif

#ifndef ROSEN_KEELS
    void SetScreenActiveMode(ScreenId id, uint32_t modeId);
#endif // !ROSEN_KEELS

#ifndef ROSEN_KEELS
    int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height);

    RSVirtualScreenResolution GetVirtualScreenResolution(ScreenId id);

    void MarkPowerOffNeedProcessOneFrame();

    void DisablePowerOffRenderControl(ScreenId id);

    void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status);

    RSScreenModeInfo GetScreenActiveMode(ScreenId id);

    std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id);

    RSScreenCapability GetScreenCapability(ScreenId id);

    ScreenPowerStatus GetScreenPowerStatus(ScreenId id);

    RSScreenData GetScreenData(ScreenId id);

    MemoryGraphic GetMemoryGraphic(int pid);

    std::vector<MemoryGraphic> GetMemoryGraphics();
#endif // !ROSEN_KEELS

#ifdef TP_FEATURE_ENABLE
    void SetTpFeatureConfig(int32_t feature, const char* config);
#endif
private:
    friend class SurfaceCaptureCallbackDirector;
    friend class SurfaceBufferCallbackDirector;
};
} // namespace Rosen
} // namespace Keels

#endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H
